package cn.mafangui.hotel.controller.worker;

import cn.mafangui.hotel.req.CheckInReq;
import cn.mafangui.hotel.req.DirectInReq;
import cn.mafangui.hotel.req.UserQueryReq;
import cn.mafangui.hotel.entity.CheckIn;
import cn.mafangui.hotel.entity.Order;
import cn.mafangui.hotel.entity.User;
import cn.mafangui.hotel.enums.ExceptionEnum;
import cn.mafangui.hotel.enums.OrderStatus;
import cn.mafangui.hotel.exception.HotelException;
import cn.mafangui.hotel.response.AjaxResult;
import cn.mafangui.hotel.response.ResponseTool;
import cn.mafangui.hotel.service.CheckInService;
import cn.mafangui.hotel.service.OrderService;
import cn.mafangui.hotel.service.UserService;
import cn.mafangui.hotel.utils.AssertUtils;
import cn.mafangui.hotel.utils.PropInfoConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Api( tags = {"操作员-入住登记"})
@RestController
@RequestMapping(value = "/op/checkIn")
public class OpCheckInController {

    @Autowired
    private CheckInService checkInService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "直接入住", notes = "用于直接前台入住，没有在系统预约的用户,除了身份信息还需要传入 roomTypeId 和 roomType 还有 订单费用;")
    @PostMapping("/directIn")
    public AjaxResult directIn(DirectInReq req) throws HotelException {
        // 1. 查询用户是否存在
        UserQueryReq userQueryReq = new UserQueryReq();
        userQueryReq.setName(req.getName());
        userQueryReq.setPhone(req.getPhone());
        userQueryReq.setIdCard(req.getIdCard());
        List<User> userList =  userService.query(userQueryReq);
        AssertUtils.assertTrue(!CollectionUtils.isEmpty(userList), ExceptionEnum.USER_NOT_FOUND);
        User user = userList.get(0);

        // 2. 生成订单 - 此处需要传入 roomTypeId 和 roomType 还有 订单费用;
        Order order = new Order(2, "直接入住", user.getUserId(),user.getName(),user.getPhone(),req.getRoomTypeId(),
                req.getRoomType(),req.getStartDate(),req.getDays(), OrderStatus.CHECK_IN.getCode(), req.getOrderCost());
        int n = orderService.addOrder(order);
        AssertUtils.assertTrue(n > 0, ExceptionEnum.ORDER_ADD_FAIED);
        // 通过自动生成的主键生成的
        AssertUtils.assertTrue(!ObjectUtils.isEmpty(order) && order.getOrderId() > 0, ExceptionEnum.ORDER_ADD_FAIED);
        Integer orderId = order.getOrderId();
        System.out.println("生成订单之后的 订单id:" + orderId);

        // 3. 根据订单来入住登记
        CheckIn checkIn = new CheckIn();
        checkIn.setOrderId(orderId);
        checkIn.setPeoCount(req.getPeoCount());
        checkIn.setPersons(req.getName());
        checkIn.setIds(req.getIdCard());
        return ResponseTool.success(checkInService.checkIn(checkIn));
    }

    @ApiOperation(value = "入住")
    @PostMapping(value="/in")
    public AjaxResult addCheckIn(CheckInReq checkInReq) throws HotelException {
        CheckIn checkIn = new CheckIn();
        checkIn.setOrderId(checkInReq.getOrderId());
        checkIn.setPeoCount(checkInReq.getPeoCount());
        checkIn.setPersons(checkInReq.getUserId());
        checkIn.setIds(checkInReq.getIds());
        return ResponseTool.success(checkInService.checkIn(checkIn));
    }

    @ApiOperation(value ="退房")
    @PostMapping(value = "/out")
    public AjaxResult checkOut(String roomNumber) throws HotelException {
        if(checkInService.checkOut(roomNumber)<0) {
            return ResponseTool.failed("退房失败");
        }
        return ResponseTool.success(PropInfoConstant.CHECK_OUT_SUCCESS);
    }


    @ApiOperation(value = "删除入住信息")
    @RequestMapping(value = "/delete")
    public AjaxResult deleteCheckIn(int checkId){
        if(checkInService.delete(checkId)!=1) {
            return ResponseTool.failed("删除失败");
        }
        return ResponseTool.success(PropInfoConstant.DELETE_SUCCESS);
    }

    @ApiOperation(value = "更新入住信息")
    @RequestMapping(value = "/update")
    public AjaxResult update(int checkId,String roomNumber){
        CheckIn checkIn = new CheckIn();
        checkIn.setCheckInId(checkId);
        checkIn.setRoomNumber(roomNumber);
        if(checkInService.update(checkIn)!=1) {
            return ResponseTool.failed("更新失败");
        }
        return ResponseTool.success(PropInfoConstant.UPDATE_SUCCESS);
    }


    @ApiOperation(value = "查询入住信息")
    @RequestMapping(value = "/{checkId}")
    public AjaxResult getById(@PathVariable int checkId){
        return ResponseTool.success(checkInService.selectById(checkId));
    }

    @ApiOperation(value = "得到所有入住信息")
    @RequestMapping(value = "")
    public AjaxResult getAll(){
        return ResponseTool.success(checkInService.selectAll());
    }

}
